<?php
/***
* +----------------------------------------------------------------------+
* | DATE: 2008-5-15                                                      |
* +----------------------------------------------------------------------+
* | Author: YuYii <fin1983@Hotmail.com>                                  |
* +----------------------------------------------------------------------+
*/


if (!isset($_GET['action'])) {
	header("HTTP/1.1 403 Forbidden");
	exit();
}

include("../class/operation.php");

$costs_c = new costs;
switch($_GET['action']) {
	case "getOut":
		$costs_c->getOut();break;
	case "setOut":
		$costs_c->setOut();break;
	case "getDateTips":
		$costs_c->getDateTips();break;
}


class costs {

	//时间用GET提交，非必要参数
	function getOut() {
		$from = null;
		$to = null;
		if (isset($_GET['from'])) {
			if (!valid_date($_GET['from'])) {
				exit();
			}
			$from = $_GET['from'];
		}
		if (isset($_GET['to'])) {
			if (!valid_date($_GET['to'])) {
				exit();
			}
			$to = $_GET['to'];
		}
		$act = new Operation;
		$rs = $act->getOut($from,$to);
		if (!$rs) exit();
		else {
			$rs_type = $act->getTypes('out');
			$M_rs_type = array();
			foreach($rs_type as $v) {
				$M_rs_type[$v['type_id']] = $v['type_name'];
			}
			foreach($rs as &$v){
				if (array_key_exists($v['typeid'],$M_rs_type))
					$v[3] = $M_rs_type[$v['typeid']];	//3 means typeid
				else $v[3] = "未定义";
			}
			$rs = arr2jsArr($rs);
			header("Content-type: text/html; charset=UTF-8");
			echo $rs;
		}
	}

	//必须设置from和to，用POST提交
	function setOut() {
		if (!isset($_POST['data']) || !isset($_POST['from']) || !isset($_POST['to']))	exit();
		$from = null;
		$to = null;
		if (!valid_date($_POST['from'])) {
			exit();
		}
		$from = $_POST['from'];

		if (!valid_date($_POST['to'])) {
			exit();
		}
		$to = $_POST['to'];

		$data = json2Arr($_POST['data']);
		$sql = array();
		$sql[] = "delete from account_out where createdate between '$from' and '$to'";

		foreach($data as $v){
			if ($this->checkParam($v)) {
				$sql[] = "INSERT INTO account_out(cost,createdate,typeid,description,remark) SELECT '".$v['price']."','".$v['dodate']."',(select type_id from cost_type where type_name='".$v['type']."'),'".$v['desc']."','".$v['remark']."'";
			}
			else {
				echo "{'errorInfo':'保存失败，提交内容非法。'}";
				exit();
			}
		}

		$act = new Operation;
		if ($act->setOut($sql)) {
			echo "{'success':'ok','info':'aaa'}";
		}
		else echo "{'errorInfo':'保存失败，数据库写入错误。'}";
	}

	function checkParam(&$v){
		if (!isset($v['price']) || !isset($v['dodate']) || !isset($v['type']) || !isset($v['desc']) || !isset($v['remark'])) return false;

		if (!preg_match("/^\d{1,9}$|^\d{1,9}\.\d{1,2}$/",$v['price'])) return false;
		$p = strpos($v['dodate'],'T');
		if ($p){
			$v['dodate'] = substr($v['dodate'],0,$p);
			if (!valid_date($v['dodate'])) return false;
			else return true;
		}
		return false;
	}

	function getDateTips() {
		echo "[['2008/06/12','123'],['2008/06/14','123dsf']]";
	}
}


?>